/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.database.pojo;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.aergia.vinny.util.DateUtil;
import org.w3c.dom.Element;

/**
 * @author fred
 */
@Entity
public class Scan implements DatabasePojo, Comparable<Scan> {

	private boolean active;
	private Card card;
	private String comment;
	private int id;
	private Location location;
	private Calendar scanTime;
	private Service service;
	private ServiceType serviceType;
	private Workstation workstation;

	public Scan() {
		// empty
	}

	/**
	 * @param card
	 * @param time
	 */
	public Scan(final Card card, final Workstation workstation, final Calendar time) {
		this.card = card;
		this.scanTime = time;
		this.workstation = workstation;
		this.active = true;
	}

	@Override
	public void attachAttributes(final Element element) {
		element.setAttribute("id", Integer.toString(this.getId()));
		element.setAttribute("active", Boolean.toString(this.isActive()));
		element.setAttribute("card", Integer.toString(this.getCard().getId()));
		if (this.getComment() != null) {
			element.setAttribute("comment", this.getComment());
		}
		element.setAttribute("location", Integer.toString(this.getLocation().getId()));
		element.setAttribute("scanTime", DateUtil.format(this.getScanTime()));
		if (this.getService() != null) {
			element.setAttribute("service", Integer.toString(this.getService().getId()));
		}
		if (this.getWorkstation() != null) {
			element.setAttribute("workstation", Integer.toString(this.getWorkstation().getId()));
		}
		if (this.getServiceType() != null) {
			element.setAttribute("serviceType", Integer.toString(this.getServiceType().getId()));
		}
	}

	@Override
	public int compareTo(final Scan o) {
		int compare = this.getCard().getNumber().compareTo(o.getCard().getNumber());
		if (compare == 0) {
			compare = this.getScanTime().compareTo(o.getScanTime());
		}
		return compare;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Scan)) {
			return false;
		}
		final Scan other = (Scan) obj;
		if (this.isActive() != other.isActive()) {
			return false;
		}
		if (this.getCard() == null) {
			if (other.getCard() != null) {
				return false;
			}
		} else if (!this.getCard().equals(other.getCard())) {
			return false;
		}
		if (this.getLocation() == null) {
			if (other.getLocation() != null) {
				return false;
			}
		} else if (!this.getLocation().equals(other.getLocation())) {
			return false;
		}
		if (this.getScanTime() == null) {
			if (other.getScanTime() != null) {
				return false;
			}
		} else if (!this.getScanTime().equals(other.getScanTime())) {
			return false;
		}
		if (this.getService() == null) {
			if (other.getService() != null) {
				return false;
			}
		} else if (!this.getService().equals(other.getService())) {
			return false;
		}
		return true;
	}

	/**
	 * @return the card
	 */
	@ManyToOne
	public Card getCard() {
		return this.card;
	}

	/**
	 * @return the comment
	 */
	public String getComment() {
		return this.comment;
	}

	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	/**
	 * @return the location
	 */
	@ManyToOne
	public Location getLocation() {
		return this.location;
	}

	/**
	 * @return the scanTime
	 */
	public Calendar getScanTime() {
		return this.scanTime;
	}

	/**
	 * @return the service
	 */
	@ManyToOne
	public Service getService() {
		return this.service;
	}

	/**
	 * @return the serviceType
	 */
	@ManyToOne
	public ServiceType getServiceType() {
		return this.serviceType;
	}

	/**
	 * @return the workstation
	 */
	@ManyToOne
	public Workstation getWorkstation() {
		return this.workstation;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (prime * result) + (this.isActive() ? 1231 : 1237);
		result = (prime * result) + (this.getCard() == null ? 0 : this.getCard().hashCode());
		result = (prime * result) + (this.getLocation() == null ? 0 : this.getLocation().hashCode());
		result = (prime * result) + (this.getScanTime() == null ? 0 : this.getScanTime().hashCode());
		result = (prime * result) + (this.getService() == null ? 0 : this.getService().hashCode());
		result = (prime * result) + (this.getWorkstation() == null ? 0 : this.getWorkstation().hashCode());
		return result;
	}

	@Override
	public boolean isActive() {
		return this.active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(final boolean active) {
		this.active = active;
	}

	/**
	 * @param card
	 *            the card to set
	 */
	public void setCard(final Card card) {
		this.card = card;
	}

	/**
	 * @param comment
	 *            the comment to set
	 */
	public void setComment(final String comment) {
		this.comment = comment;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(final int id) {
		this.id = id;
	}

	/**
	 * @param location
	 *            the location to set
	 */
	public void setLocation(final Location location) {
		this.location = location;
	}

	/**
	 * @param scanTime
	 *            the scanTime to set
	 */
	public void setScanTime(final Calendar scanTime) {
		this.scanTime = scanTime;
	}

	/**
	 * @param service
	 *            the service to set
	 */
	public void setService(final Service service) {
		this.service = service;
	}

	/**
	 * @param serviceType
	 *            the serviceType to set
	 */
	public void setServiceType(final ServiceType serviceType) {
		this.serviceType = serviceType;
	}

	/**
	 * @param workstation
	 *            the workstation to set
	 */
	public void setWorkstation(final Workstation workstation) {
		this.workstation = workstation;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Scan [card=" + this.getCard().getNumber() + ", scanTime=" + DateUtil.format(this.getScanTime()) + ", active=" + this.isActive() + "]";
	}

}
